
<div class="layui-fluid layui-anim febs-anim" id="notice-list" lay-title="通知列表">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="user-table-form" id="selectForm">
                        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                            <legend style="font-size: 15px; font-weight: bolder;">批量查询</legend>
                        </fieldset>
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">通知标题</label>
                                        <div class="layui-input-inline">
                                            <input type="text" id="txtTitle" name="title" autocomplete="off"
                                                   class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">创建人</label>
                                        <div class="layui-input-inline">
                                            <select name="createUserId" id="cboTeachery">
                                                <option value="0">选择全部</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain
                                            table-action" id="btnSearch" title="查询">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-green-plain
                                            table-action" id="btnReset"  title="重置">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                            </div>
                        </div>
                        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                            <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain table-action"
                                 id="btnCreateNotice">
                                <i class="layui-icon layui-icon-plus-circle">创建通知</i>
                            </div>
                        </blockquote>
                    </form>
                    <table lay-filter="noticeTable" lay-data="{id: 'noticeTable'}"></table>
                </div>

            </div>
        </div>
    </div>
</div>


<script type="text/html" id="notice-option">
    {{# if(d.status==0){}}
    <a lay-event="edit" ><i class="layui-icon febs-edit-area febs-green" title="编辑">&#xe7a4;</i></a>
    <a lay-event="release"> <i class="layui-icon febs-edit-area febs-green" title="发送">&#xe82e;</i></a>
    <a  lay-event="del" > <i class="layui-icon febs-edit-area febs-red" title="删除">&#xe7f9;</i></a>
    {{# } else if(d.status==1) { }}
    <a  lay-event="detail"><i class="layui-icon febs-edit-area febs-green" title="详情">&#xe7a5;</i></a>
    <a  lay-event="statistics" ><i class="layui-icon febs-edit-area febs-green" title="统计">&#xe831;</i></a>
    {{# } else {}}
    {{# } }}
</script>
<script type="text/html" id="notice-status">
    {{#
    let status = {
    1: {title: '已发布', color: 'green'},
    0: {title: '草稿', color: 'volcano'}
    }[d.status];
    }}
    <span class="layui-badge febs-bg-{{status.color}}">{{ status.title }}</span>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'febs', 'xmSelect','validate'], function () {
        let $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#notice-list'),
            $btnCreateNotice=$view.find('#btnCreateNotice'),
            $btnSearch=$view.find('#btnSearch'),
            $btnReset=$view.find('#btnReset'),
            $searchForm = $view.find('form'),
            $semp= $view.find('#semp'),
            sortObject = {field: 'createTime', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo;

        form.render();

        initTable();

        table.on('sort(noticeTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        //查询
        $btnSearch.on('click', function () {
            let params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        //重置
        $btnReset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            //deptXmlSelect.setValue(['']);
            tableIns.reload({where: getQueryParams(true), page: {curr: 1}, initSort: sortObject });
        });

        //创建人下拉菜单
        $.ajax({
            type:'get',
            url:ctx + 'user/selectUserlist',
            cache:false,
            success:function (res) {
                var userArray=res.data.rows
                if(userArray.length>0){
                    for(var i=0;i<userArray.length;i++){
                        var user=userArray[i];
                        $("#cboTeachery").append("<option value='"+user.userId+"'>"+user.username+"</option>");
                    }
                }
                layui.form.render("select");
                $.ajax({
                    type:'get',
                    url:'notice/findCreateUserId',
                    success:function (res) {
                        $("#cboTeachery").val(res.data);
                        layui.form.render("select");
                    }
                })
            }

        });
        //进入消息增加的页面
        $btnCreateNotice.on('click', function () {
            febs.modal.open('新建通知', 'notice/add', {
                btn: ['创建','保存草稿', '取消'],
                area: $(window).width() <= 750 ? '95%' : '50%',
                yes: function (index, layero) {
                    $('#notice-add').find('#status_Id').val(1);
                    $('#notice-add').find('#submit').trigger('click');
                    tableIns.reload();
                    // 单击之后提交按钮不可选,防止重复提交
                    var DISABLED = 'layui-btn-disabled';
                    // 增加样式
                    $('#notice-add').find('#submit').addClass(DISABLED);
                    // 增加属性
                    $('#notice-add').find('#submit').attr('disabled', 'disabled');

                    return false
                },
                btn2: function () {
                    $('#notice-add').find('#status_Id').val(0);
                    $('#notice-add').find('#submitTemp').trigger('click');
                    tableIns.reload();
                    // 单击之后提交按钮不可选,防止重复提交
                    var DISABLED = 'layui-btn-disabled';
                    // 增加样式
                    $('#notice-add').find('#submitTemp').addClass(DISABLED);
                    // 增加属性
                    $('#notice-add').find('#submitTemp').attr('disabled', 'disabled');

                    return false;
                },
                btn3: function () {
                    $('#notice-add').find('#reset').trigger('click');
//                    return false;
                }
            });
        });


        $semp.on('click',function () {
            var selectUser= febs.modal.open('选择员工', 'businesses/user', {
                area: ["95%","95%"],
                btn: ['选择勾选结果', '选择本页结果','选择全部结果'],
                yes: function (index, layero) {
                    $('#select-user').find('#btnCheckResults').trigger('click');
                    // 单击之后提交按钮不可选,防止重复提交
                    var DISABLED = 'layui-btn-disabled';
                    // 增加样式
                    $('#select-user').find('#btnCheckResults').addClass(DISABLED);
                    // 增加属性
                    $('#select-user').find('#btnCheckResults').attr('disabled', 'disabled');
                },
                btn2: function (index, layero) {
                    $('#select-user').find('#btnPageResults').trigger('click');
                    // 单击之后提交按钮不可选,防止重复提交
                    var DISABLED = 'layui-btn-disabled';
                    // 增加样式
                    $('#select-user').find('#btnPageResults').addClass(DISABLED);
                    // 增加属性
                    $('#select-user').find('#btnPageResults').attr('disabled', 'disabled');
                },
                btn3: function (index, layero) {
                    $('#select-user').find('#btnAllResults').trigger('click');
                    // 单击之后提交按钮不可选,防止重复提交
                    var DISABLED = 'layui-btn-disabled';
                    // 增加样式
                    $('#select-user').find('#btnAllResults').addClass(DISABLED);
                    // 增加属性
                    $('#select-user').find('#btnAllResults').attr('disabled', 'disabled');
                },
                success: function(layero){
                    layero.find('.layui-layer-btn').css('text-align', 'center');
                },
            });
        });

        function initTable() {
            console.log("initTable be called "+$("#cboTeachery").val())
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'noticeTable',
                url: ctx + 'notice/foundNoticeSelect',
                cols: [[
                    {field: 'id',              title: '编号',    width: 100},
                    {field: 'title',           title: '通知标题', minWidth: 240},
                    {field: 'userName',        title: '创建人'},
                    {field: 'createTime',      title: '创建时间', minWidth: 180},
                    {field: 'countShouldRead', title: '应读',    width: 100},
                    {field: 'countRead',       title: '已读',    width: 100},
                    {title: '状态',            templet: '#notice-status', width: 100},
                    {field: 'status',title: '操作', toolbar: '#notice-option', minWidth: 165},
                ]],
            });
        }

        table.on('tool(noticeTable)',function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'edit'){
                febs.modal.open('编辑通知', 'businesses/noticeEdit/' + data.id, {
                    btn: ['确定','取消'],
                    area: $(window).width() <= 750 ? '95%' : '50%',
                    yes: function (index, layero) {
                        $('#notice-edit').find('#btnPreservation').trigger('click');
                        // 单击之后提交按钮不可选,防止重复提交
                        var DISABLED = 'layui-btn-disabled';
                        // 增加样式
                        $('#notice-edit').find('#btnPreservation').addClass(DISABLED);
                        // 增加属性
                        $('#notice-edit').find('#btnPreservation').attr('disabled', 'disabled');
                    },
                    btn2: function (index, layero) {

                    }
                });
            }

            if (layEvent === 'del'){
                febs.modal.confirm('删除通知', '确定删除该通知？', function () {
                    console.log(data.id)
                    deleteNotice(data.id)
                    tableIns.reload();
                });
            }

            /**
             * 修改
             */
            if (layEvent === 'release'){
                febs.modal.confirm('确定发送', '确定发送该通知？', function () {
                    console.log(data)
                    $.ajax({
                        type:'post',
                        url:'notice/update',
                        data:{"id":data.id,"classId":data.classId},
                        success:function (res) {
                            console.log("回来了")
                            tableIns.reload();
                        }
                    })
                });
            }

            if (layEvent === 'detail'){
             var index= febs.modal.open('通知详情', 'businesses/noticeDetail/' + data.id, {
                 area: $(window).width() <= 750 ? '95%' : '50%',
                  btn: [ '确定','取消'],
                    yes: function (index, layero) {
                        layer.closeAll()
                    },
                    btn2: function () {

                    }
                });
            }

            if (layEvent === 'statistics'){
                febs.modal.open('统计详情', 'noticeUser/statistics/' + data.id, {
                    // area: $(window).width() <= 750 ? '90%' : '50%',
                    area: ['70%', '80%'],
                    btn: [ '取消'],

                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }


        })


        function getQueryParams(flag) {
            console.log("getQueryParams values is "+flag);
            let batchName=$("#cboBatchy").val();
            let collegeName=$("#cboCollegey").val();
            let majorName=$("#cboMajory").val();
            let levelName=$("#cboLevely").val();
            let title=$("#txtTitle").val();
            let params = $searchForm.serializeJson();
            if (flag) {

            }
            let createUserId=$("#cboTeachery").val();
            params.createUserId=createUserId;
            params.batchId=batchName;
            params.collegeName=collegeName;
            params.majorName=majorName;
            params.levelName=levelName;

            params.title=title;
            let createTime = params.time;
            if (createTime) {
                createTimeFrom = createTime.split(' - ')[0];
                createTimeTo = createTime.split(' - ')[1];
            }
            params.invalidate_ie_cache = new Date();
            return params;
        }

        function deleteNotice(noticeId){
            febs.get(ctx + 'notice/deleteById/' + noticeId, null, function () {
                febs.alert.success('删除通知成功');
                $btnSearch.click();
            });
        }

        //按回车搜索
        $(".layui-input").keydown(function (e) {//当按下按键时
            if (e.which == 13) {//.which属性判断按下的是哪个键，回车键的键位序号为13
                $btnSearch.click();
            }
        });

    })
</script>